<?php

namespace App\Http\Controllers\Api;
use App\Base\OrderStatus;
use App\Base\Power;
use App\Base\TaskState;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

/**
 * Created by PhpStorm.
 * User: Dream
 * Date: 2018/2/1
 * Time: 23:33
 */
class ApiController extends Controller
{
    public function login()
    {
        $input = Input::only('user_phone','user_password');
        //$input['user_phone'] = 123456;
        //$input['user_password'] =123456;
        $input['user_password'] = md5($input['user_password']);
        //dd($input);
        $user = DB::table('user')
            ->where('user_phone',$input['user_phone'])
            ->value('user_password');
        if($user == NULL)
        {
            return response()->json([
                'statue'=>404,
                'message'=>'您没有注册,请先注册'
            ]);
        }
        else
        {
            $userDate = DB::table('user')
                ->where('user_phone',$input['user_phone'])
                ->where('user_password',$input['user_password'])
                ->first();
            //dd($userDate);
            if($userDate == NULL)
            {
                return response()->json([
                    'status'=>500,
                    'message'=>'密码错误'
                ]);
            }
            else
            {
                return response()->json([
                    'status'=>200,
                    'message'=>'登录成功',
                    'data'=>$userDate
                ]);
            }
        }
    }


    public function register()
    {
        $data = Input::only('user_name','user_phone');
        //dd($data);
        $password = Input::only('user_password');
        $data['user_password'] = md5($password['user_password']);
        $check = DB::table('user')
            ->where('user_phone',"=",$data['user_phone'])
            ->get();
        if($check == NULL)
        {
            DB::table('user')->insert($data);
            return response([
                'status'=>200,
                'message'=>'注册成功'
            ]);
        }
        else
        {
            return response([
                'status'=>500,
                'message'=>'注册失败'
            ]);
        }
    }

    public function drugData()
    {
        $data = DB::table('re_drug_business')
            ->leftJoin('drug','drug_id','=','re_drug_id')
            ->leftJoin('business','business_id','=','re_business_id')
            ->get();
        return response()->json([
            'status'=>200,
            'message'=>'获取数据成功',
            'data'=>$data
        ]);
    }

    public function drugInfo()
    {
        $input = Input::only('drug_id');
        $data = DB::table('re_drug_business')
            ->leftJoin('drug','drug_id','=','re_drug_id')
            ->leftJoin('business','business_id','=','re_business_id')
            ->where('re_drug_id',$input['drug_id'])
            ->get();
        return response()->json([
            'status'=>200,
            'message'=>'获取数据成功',
            'data'=>$data
        ]);
    }

    public function businessData()
    {
        $input = Input::only('business_id');
        //$input['business_id'] = 1;
        $data = DB::table('re_drug_business')
            ->where('re_business_id','=',$input['business_id'])
            ->leftJoin('drug','drug_id','=','re_drug_id')
            ->leftJoin('business','business_id','=','re_business_id')
            ->get();
        return response()->json([
            'status'=>200,
            'message'=>'获取数据成功',
            'data'=>$data
        ]);
    }

    public function orderDrug(OrderStatus $orderStatus)
    {
        $value = Input::only("drug_id",'business_id','user_id');
        $input['re_business'] = $value['business_id'];
        $input['re_drug_id'] = $value['drug_id'];
        $input['re_user_id'] = $value['user_id'];
        $input['re_user_drug_status'] = $orderStatus->unOrder()->status_id;
        $input['re_user_drug_time'] = date('Y-m-d H:i:s');
        DB::table('re_user_drug')->insert($input);
        return response([
            'status'=>200,
            'message'=>'预定成功'
        ]);
    }

    public function myOrder()
    {
        $input = Input::only('user_id');
        //$input['user_id'] = 1;
        $data = DB::table('re_user_drug')
            ->leftJoin('drug','drug_id','=','re_drug_id')
            ->leftJoin('status','status_id','=','re_user_drug_status')
            ->where('re_user_id','=',$input['user_id'])
            ->get();
        return response()->json([
            'status'=>200,
            'message'=>'获取数据成功',
            'data'=>$data
        ]);
    }

    public function orderBusiness()
    {
        $input['business_id'] = Input::only('business_id');
        $input['user_id'] = Input::only('user_id');
        DB::table('re_user_business')->insert($input);
        return response([
            'status'=>200,
            'message'=>'收藏成功'
        ]);
    }

    public function myorderBusiness()
    {
        $input = Input::only('user_id');
        $input['user_id'] = 1;
        $data = DB::table('re_user_business')
            ->join('business','business.business_id','=','re_user_business.business_id')
            ->where('user_id','=',$input['user_id'])
            ->get();
        return response([
            'status'=>200,
            'message'=>'获取数据成功',
            'data'=>$data
        ]);
    }

    public function searchDrug()
    {
        $input = Input::only('drug_symptom');
        $drug = $input['drug_symptom'];
        $data = DB::table('re_drug_business')
            ->leftJoin('drug','drug_id','=','re_drug_id')
            ->leftJoin('business','business_id','=','re_business_id')
            ->where('drug_symptom', 'like', "%$drug%")
            ->get();
        return response([
            'status'=>200,
            'message'=>'获取数据成功',
            'data'=>$data
        ]);
    }
}